<?php
/**
 * 描述：会员管理中心-编辑采购信息。
 * @final
 */
final class Exec_MC_Procurement_Edit extends Exec_MC_Master
{   
    public function Get()
    {
        #获取参数
        $id=Functions::UrlArgs('id');

        #获取数据
        $data=DB::GetRowByID('procurement',$id);
        $data['IsUrgent']=($data['IsUrgent']==1 ? 'yes':'no');
        $data['ThumbnailLocation']=Config::PATH_RELATIVE_ATTACHMENT_PROCUREMENT.'/'.$data['ThumbnailPath'];
        $data['Validity']=date('Y-m-d',  strtotime($data['Validity']));
        $data['Datetime']=date('Y-m-d',  strtotime($data['Datetime']));
        $data['ValidityValue']=(strtotime($data['Validity'])-strtotime($data['Datetime']))/3600/24;
        
        #渲染模板
        View::Assign('Data',$data);
        $this->MasterPage_Display(View::Fetch('mc/procurement/edit.tpl'));
    }

    public function Post()
    {
        #获取参数
        $id=Functions::FormArgs('ID');

        #准备数据
        $data=array
        (
            'ClassID'=>Functions::FormArgs('ClassID'),
            'SubclassID'=>Functions::FormArgs('SubclassID'),
            'Title'=>Functions::FormArgs('Title'),
            'Norms'=>Functions::FormArgs('Norms'),
            'Brand'=>Functions::FormArgs('Brand'),            
            'Origin'=>Functions::FormArgs('Origin'),
            'Description'=>Functions::FormArgs('Description'),
            'SupplierLocation'=>Functions::FormArgs('SupplierLocation'),
            'Amount'=>Functions::FormArgs('Amount'),
            'Unit'=>Functions::FormArgs('Unit'),
            'TakeAddress'=>Functions::FormArgs('TakeAddress'),
            'Validity'=>Functions::FormArgs('Validity'),
            'IsUrgent'=>Functions::FormArgs('IsUrgent')=='yes' ? 1:0
        );
        
        #数据验证
        //服务端数据验证

        #上传图片
        $result=Upload::Probe($_FILES['ThumbnailPath'],Config::UPLOAD_ATTACHMENT_SIZE_IMAGE,Config::UPLOAD_ATTACHMENT_EXT_IMAGE,Config::PATH_UPLOAD_ATTACHMENT_PROCUREMENT);
        if($result['error']!=UPLOAD_ERR_NO_FILE)
        {
            if($result['error']==UPLOAD_ERR_OK)
            {
                $data['ThumbnailPath']=Upload::Execute($_FILES['ThumbnailPath'],Config::PATH_UPLOAD_ATTACHMENT_PROCUREMENT);
            }
            else
            {
                if($result['error']==Upload::UPLOAD_ERR_CUSTOM_EXT)
                {
                    echo 'Thumbnail_Ext:'.Config::UPLOAD_ATTACHMENT_EXT_IMAGE;
                    return;
                }
                else if($result['error']==Upload::UPLOAD_ERR_CUSTOM_SIZE)
                {
                    echo 'Thumbnail_Size:'.Functions::SizeFormat(Config::UPLOAD_ATTACHMENT_SIZE_IMAGE);
                    return;
                }
                else
                {
                    echo 'Thumbnail';
                    return;
                }
            }
        }
        
        #插入数据
        DB::UpdateRowByID('procurement',$id,$data);
        echo 'Success';
    }
}
?>